import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;


public class CSVParser {

	public CSVParser() {
	}
	public static ArrayList<Item> parseAndReturnListOfItems(File csvFile) {

		File fXmlFile = csvFile;
		BufferedReader br = null;
		ArrayList<Item> ll = new ArrayList<Item>();
		try {
			br = new BufferedReader(new FileReader(fXmlFile));
			String line = null;
			Item item = null;
			while ((line = br.readLine()) != null) {

				System.out.println(line);
				String[] words = line.trim().split(";");
				System.out.println(words.length);
				item=new Item();
				item.setTermekNev(words[0]);
				System.out.print(item.getTermekNev()+ " ");
				String[] tomeg= words[1].trim().split(" ");
				if ("g".equals(tomeg[1])) {
					item.setTomeg(Float.parseFloat(tomeg[0])/1000 +"");
				} else {
					item.setTomeg("");
				}
				System.out.print(item.getTomeg()+ " ");		
				item.setNettoAr(words[5]);
				System.out.print(item.getNettoAr()+ " ");
				item.setBruttoAr(words[6]);
				System.out.print(item.getBruttoAr()+ " ");
				item.setEgyseg(words[7]);
				System.out.println(item.getEgyseg()+ " ");
				
				ll.add(item);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}


		return ll;
	}
	public static ArrayList<Item> parseExportedAndReturnListOfItems(File csvFile) {

		File fXmlFile = csvFile;
		BufferedReader br = null;
		ArrayList<Item> ll = new ArrayList<Item>();
		try {
			br = new BufferedReader(new FileReader(fXmlFile));
			String line = null;
			Item item = null;
			if ((line = br.readLine()) != null) {}


			while ((line = br.readLine()) != null) {

				System.out.println(line);
				String[] words = line.trim().split(";");
				for (int i = 0; i< words.length;i++) {
					words[i] = words[i].replaceAll("\"","");
				}
				item=new Item();
				item.setCikkSzam(words[0]);
				System.out.print(item.getCikkSzam()+ " ");
				item.setTermekNev(words[1]);
				System.out.print(item.getTermekNev()+ " ");
				item.setNettoAr(words[3]);
				System.out.print(item.getNettoAr());
				item.setBruttoAr(words[4]);
				System.out.println(item.getBruttoAr());
				ll.add(item);

			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}


		return ll;
	}
}